package com.zn.net
{
    import com.zn.log.Log;

    import flash.net.Responder;
    import flash.utils.clearTimeout;
    import flash.utils.setTimeout;

    public class Responder extends flash.net.Responder
    {
        //2分钟
        public static const TIME_OUT:int = 120000;

        private var _resultFunction:Function;

        private var _failedFunction:Function;

        private var _method:String;

        private var _timeoutId:int;

        public function Responder(method:String, result:Function, status:Function = null)
        {
            super(resultHandler, failedHandler);

            _method = method;
            _resultFunction = result;
            _failedFunction = status;

            _timeoutId = setTimeout(timeOutHandler, TIME_OUT);
        }

        protected function resultHandler(value:* = null):void
        {
            Log.debug(com.zn.net.Responder, "resultHandler", value);
            clearTimeout(_timeoutId);

            if (_resultFunction == null)
                return;

            _resultFunction.call(this, value);
        }

        protected function failedHandler(value:* = null):void
        {
            Log.error(com.zn.net.Responder, "failedHandler", value);
            clearTimeout(_timeoutId);

            if (_failedFunction == null)
                return;

            _failedFunction.call(this, value);
        }

        protected function timeOutHandler():void
        {
            Log.error(com.zn.net.Responder, "timeOutHandler", " 请求超时");
            if (_failedFunction == null)
                return;

            _failedFunction();
        }
    }
}